/**
 * Created by 朱理哲 on 17/8/14.
 */
import React, {Component} from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    TextInput,
    View,
    PixelRatio
} from 'react-native';
import {GetOrderList} from './OrderNet'
import {observable, action, computed, observe} from 'mobx'

class ProductM {
    constructor(p) {
        this.currentPrice = p.currentPrice;
        this.name = p.name;
        this.volume = p.volume;
        this.originalPrice = p.originalPrice;
        this.productItemId = p.productItemId;
        this.saleMount = p.saleMount;
        this.selectCount = p.selectCount;
        this.productImage = p.productImage;
    }
}

class OrderM {
    constructor(order) {
        this.createTime = order.createTime;
        this.deliveryId = order.deliveryId;
        this.deliveryerName = order.deliveryerName;
        this.deliveryerPhoneNum = order.deliveryerPhoneNum;
        this.orderId = order.orderId;
        this.orderNo = order.orderNo;
        this.shopId = order.shopId;
        this.productItemModels = [];
        order.productItemModels.forEach((p, index) => {
            this._addShopM(p);
        });
        this.shopName = order.shopName;
        this.status = order.status;
        this.totalCount = order.totalCount;
        this.totalPayRmb = order.totalPayRmb;
    }

    _addShopM(product) {
        let productM = new ProductM(product);
        this.productItemModels.push(productM)
    }

    @action setOrderStatus(status){
        this.status = status;
    }
}

class OrderListStore {
    @observable _allList = [];
    @observable _createList = [];
    @observable _deliveryList = [];
    @observable _appraiseList = [];
    @observable _listArray = [this._allList, this._createList, this._deliveryList, this._appraiseList];

    // 正在加载
    @observable loading = false ;

    @computed get allList(){
        return this._allList ;
    }
    @computed get createList(){
        return this._createList ;
    }
    @computed get deliveryList(){
        return this._deliveryList ;
    }
    @computed get appraiseList(){
        return this._appraiseList;
    }

    @action
    async refreshList(pageIndex, pageNo, orderStatus) {
        this.loading = true ;
        let order = await GetOrderList(pageNo, orderStatus);
        let array = [];
        if (order == null) {
            order = [];
        }
        order.map((item) => {
            let order = new OrderM(item);
            array.push(order);
        });
        if (pageIndex==0) {
            this._allList=array;
        } else if (pageIndex==1) {
            this._createList = array;
        } else if (pageIndex==2) {
            this._deliveryList = array;
        } else if (pageIndex==3) {
            this._appraiseList = array;
        }
        this.loading = false ;
    }
    @action getCurrentList(pageIndex) {
        return this._listArray[pageIndex];
    }

    @action
    SignOrder(orderId){
        console.log('JessieJ订单签收++++++++++'+orderId);
        // this._listArray[pageIndex] = this._listArray[pageIndex].filter((orderM)=>orderM.orderId!=orderId);
        this._deliveryList = this._deliveryList.filter((orderM)=>orderM.orderId!=orderId);
    }

}

const OrderStore = new OrderListStore();
export default OrderStore;